package kr.jin.zeroprojectone;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class FragmentDialog extends DialogFragment {
	
	private AlertDialog.Builder dialog;
	private LayoutInflater layoutinflater;
	private EditText edit;
	boolean inputAct = true;
	private String lastAct;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		String startActName = getResources().getString(R.string.class_fragmentdialog_start_actname);
		String dialogTitle = getResources().getString(R.string.class_fragmentdialog_dialogtitle1);
		String dialogMessage = getResources().getString(R.string.class_fragmentdialog_dialogmessage1);
		String start = getResources().getString(R.string.class_fragmentdialog_start);
		String restart = getResources().getString(R.string.class_fragmentdialog_restart);
		String stop = getResources().getString(R.string.class_fragmentdialog_stop);
		String cancel = getResources().getString(R.string.class_fragmentdialog_cancel);
		String save = getResources().getString(R.string.class_fragmentdialog_save);
		
		dialog = new AlertDialog.Builder(getActivity());
		layoutinflater = getActivity().getLayoutInflater();
		View view1 = layoutinflater.inflate(R.layout.fragment_dialog1, null);
			
		edit = (EditText)view1.findViewById(R.id.fragment_dialog1_edittext);
		dialog.setTitle(dialogTitle);
		dialog.setMessage(dialogMessage);
		dialog.setView(view1);	
		//입력한 활동명을 프레퍼런스에서 불러옴
		SharedPreferences pref = getActivity().getSharedPreferences("inputAct", 0);
		lastAct = pref.getString("LastAct", startActName);
		edit.setText(lastAct);
					
		//버튼
		switch(MainTime.timeStatus){
			case MainTime.START:	
				dialog.setPositiveButton(start, new OnClickListener() {		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						((MainTime)getActivity()).doPositiveClick(1,edit.getText().toString());	
						//입력한 활동명을 프레퍼런스에 저장
						SharedPreferences pref = getActivity().getSharedPreferences("inputAct", 0);
						SharedPreferences.Editor editor = pref.edit();
						editor.putString("LastAct", edit.getText().toString());
						editor.commit();					
					}
				});	
				dialog.setNegativeButton(cancel, null);
				break;
			case MainTime.RUNNING:			
				edit.setFocusable(false);		
				dialog.setPositiveButton(stop, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						((MainTime)getActivity()).doPositiveClick(1,edit.getText().toString());	
				
					}
				});			
				dialog.setNegativeButton(cancel, null);
				break;
			case MainTime.PAUSE:		
				edit.setFocusable(false);			
				dialog.setPositiveButton(restart, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						((MainTime)getActivity()).doPositiveClick(1,edit.getText().toString());	
					}
				});	
				dialog.setNeutralButton(save, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						((MainTime)getActivity()).doNeutralClick(1,edit.getText().toString());							
					}
				});
				dialog.setNegativeButton(cancel, null);		
		}
		return dialog.create();	
	}
}

